﻿using RealTimeCharts.LineChart;
using RemoteData.core.queue;
using RemoteData.utils;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security.RightsManagement;
using System.Text;

namespace RemoteData.core.Listener
{
    /// <summary>
    /// 波长消息
    /// </summary>
    public class Wave : QueueData
    {
        public string serialNo;
        public int nChannel;
        public byte cmd; // 存储协议
        public ChannelGrating[] chGratting;
    }
    /// <summary>
    /// 每个通道的光栅波长
    /// </summary>
    public struct ChannelGrating
    {
        public int chID; // 通道号
        public int ngrating; // 光栅数
        public char reserved1;
        public char reserved2;
        public double[] waveValueArray; // 光栅的波长数据
        public double[] dBmValue;  // 强度数据
    }

    public class SpectrumData : QueueData
    {
        public string serialNo;
        public string version;
        public ushort nChannel;
        public short forwardVersion;
        public double temperature;
        public int runTime;
        public int channelNo; // 通道号
        public int ngrating; // 光栅数
        public double[] dBmValue;  // 强度数据
        public double[] waveValueArray; // 光栅的波长数据
        public List<ValuePoint> ValuePoints; // 光谱数据
    }

    public class OPM_Protocol
    {
        static public int ParseWaveLenPackage(byte[] buffer,int msgLne, ref Wave waveLen)
        {
            int offset = 0;
            offset += 2;
            waveLen.chGratting = new ChannelGrating[16];
            waveLen.nChannel = 16;

            for(int i = 0; i < 16; i++)
            {
                waveLen.chGratting[i].ngrating = 8;
                waveLen.chGratting[i].waveValueArray = new double[8];
                for(int j = 0; j < 8; j++)
                {
                    waveLen.chGratting[i].waveValueArray[j] = DataConverter.ToInt16(buffer, offset) / 1000.0 + 1520;
                    offset += 2;
                }
            }

            return 0;
        }

        static public int ParseSpectrumPackage(byte[] buffer, int msgLen, ref SpectrumData spectrumData)
        {
            int offset = 0;

            // 序列号
            int len = GetASCIIStrLength(buffer, offset, 16);
            spectrumData.serialNo = Encoding.ASCII.GetString(buffer, offset, len);
            offset += 16;

            // 软硬件版本
            len = GetASCIIStrLength(buffer, offset, 16);
            spectrumData.version= Encoding.ASCII.GetString(buffer, offset, len);
            offset += 16;

            // 通道数
            spectrumData.nChannel = BitConverter.ToUInt16(buffer, offset);
            offset += 2;

            // 转发器版本
            spectrumData.forwardVersion = BitConverter.ToInt16(buffer, offset);
            offset += 2;

            // 当前温度
            spectrumData.temperature = BitConverter.ToInt16(buffer, offset) / 100d;
            offset += 2;

            // 运行分钟
            spectrumData.runTime = BitConverter.ToUInt16(buffer, offset);
            offset += 2;

            // 预留
            offset += 20;

            // 通道号
            spectrumData.channelNo = buffer[offset]; offset += 1;

            // 光栅数
            spectrumData.ngrating = buffer[offset]; offset += 1;

            spectrumData.dBmValue = new double[spectrumData.ngrating];
            spectrumData.waveValueArray = new double[spectrumData.ngrating];

            for(int i = 0; i < spectrumData.ngrating; i++)
            {
                spectrumData.waveValueArray[i] = DataConverter.ToInt32(buffer,offset + i*4)/1000d;

                byte dataL = buffer[offset + 120 + i*2];
                byte dataH = buffer[offset + 120 + i*2+1];
                UInt16 uValue = (UInt16)(dataL + dataH * 256);
                Int16 value = (Int16)uValue;
                spectrumData.dBmValue[i] = value / 10.0;
            }

            offset += 184;

            spectrumData.ValuePoints = new List<ValuePoint>();
            for(int i = 0; i < 1001; i++)
            {
                byte dataL = buffer[offset]; offset += 1;
                byte dataH = buffer[offset]; offset += 1;
                UInt16 uValue = (UInt16)(dataL + dataH * 256);
                Int16 value = (Int16)uValue;

                spectrumData.ValuePoints.Add(new ValuePoint(1525.0+i*0.04,value/10.0));
            }

            return 0;
        }

        /// <summary>
        /// 获取字节数组中字符串长度
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="maxSize"></param>
        /// <returns></returns>
        public static int GetASCIIStrLength(byte[] buffer, int offset, int maxSize)
        {
            int len = 0;
            for (int i = offset; i < offset + maxSize; i++)
            {
                if (buffer[i] != 0)
                {
                    len++;
                }
                else
                {
                    break;
                }
            }
            return len;
        }
    }
}
